<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
        }
        .container {
            width: 100vw;
            height: 100vh;
            display: grid;
        }
        .inner-container {
            outline: 1px solid brown;
        }
        .ball {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: blue;
        }
        .ball:active {
            background-color: blueviolet;
        }
    </style>
</head>
<body>
    <div class="container">
        
    </div>

    <script src="../../../demo/jq/jq.js"></script>
    <script>
        const info = prompt('请输入行列，中间用逗号隔开');
        // const arr = info.split(',');
        // const colCount = arr[1];
        // const rowCount = arr[0];
        // 数组解构
        const [rowCount, colCount] = info.split(',');
        
        const container = $('.container');
        container.css({
            'grid-template-columns': `repeat(${ colCount }, 1fr)`, 
            'grid-template-rows': `repeat(${ rowCount }, 1fr)`, 
        });
        for (let i = 0; i < colCount * rowCount; i++) {
            const innerContainer = $(`
                <div class="inner-container">
                    <div class="ball"></div>
                </div>
            `);
            container.append(innerContainer);
        }

        const ball = $('.ball');
        let isMouseDown = false;
        let innerX = 0;
        let innerY = 0;
        let parentX = 0;
        let parentY = 0;
        ball.on('mousedown', function (e) {
            isMouseDown = true;
            innerX = e.offsetX;
            innerY = e.offsetY;
            parentX = $(this).parent().offset().left;
            parentY = $(this).parent().offset().top;
        });
        $(document).on('mouseup', function () {
            isMouseDown = false;
        });
        $(document).on('mousemove', function (e) {
            // 禁止默认事件
            e.preventDefault();
            if (!isMouseDown) {
                return;
            }
            // 针对父元素的位置
            let x = e.clientX - innerX - parentX;
            let y = e.clientY - innerY - parentY;
            // 判断针对父元素的边界
            if (x < 0) {
                x = 0
            }
            if (y < 0) {
                y = 0;
            }
            const xMax = window.innerWidth * (1 / colCount) - 100;
            const yMax = window.innerHeight * (1 / rowCount) - 100;
            if (x > xMax) {
                x = xMax;
            }
            if (y > yMax) {
                y = yMax;
            }

            // const xMax = window.innerWidth * .5 - 100;
            // const yMax = window.innerHeight * .5 - 100;
            // let x = e.clientX - innerX;
            // x = x < 0
            //     ? 0
            //     : x > xMax
            //         ? xMax
            //         : x;
            // let y = e.clientY - innerY;
            // y = y < 0
            //     ? 0
            //     : y > yMax
            //         ? yMax
            //         : y;
            ball.css({ transform: `translate(${ x }px, ${ y }px)` });
        });
    </script>
</body>
</html>